package com.stone.back.order.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.stone.back.order.entity.BfOrder;
import com.stone.back.order.service.IOrderService;
import com.stone.base.controller.BaseController;
import com.stone.base.entity.Page;

/**
 * 订单管理
 * @author Stone P
 *
 */
@Controller
@RequestMapping("/backOrderCtl")
public class OrderController extends BaseController {

	@Autowired
	private IOrderService orderService;
	
	/**
	 * 查询订单分页信息
	 * @param pageNumber			页数
	 * @param priceMin				订单总价
	 * @param priceMax				订单总价
	 * @param startCreateDate		订餐时间
	 * @param endCreateDate			订餐时间
	 * @param status				订单状态
	 * @return
	 */
	@RequestMapping("/select")
    public @ResponseBody Page selectOrderPage(String pageNumber, String priceMin, String priceMax, String startCreateDate, String endCreateDate, String status, HttpServletRequest request){
		if(StringUtils.isBlank(pageNumber)) {
			pageNumber = "1";
		}
		Page page = this.orderService.selectOrderPage(Integer.parseInt(pageNumber), priceMin, priceMax, startCreateDate, endCreateDate, status);
		return page;
	}
	
	/**
	 * 查去订单菜品详情页
	 * @param orderId	订单id	
	 * @return
	 */
	@RequestMapping("/toInfo")
    public ModelAndView toOrderDishInfo(String orderId, HttpServletRequest request){
		ModelAndView mv = new ModelAndView("/background/order/orderinfolist");
		mv.addObject("orderId", orderId);
		return mv;
	}
	
	/**
	 * 查询订单详情分页信息
	 * @param pageNumber	页数
	 * @param name			名称
	 * @param datatype		类型
	 * @param priceMax		单价
	 * @param priceMin		单价	
	 * @return
	 */
	@RequestMapping("/selectInfo")
    public @ResponseBody Page selectDishInfoPage(String pageNumber, String orderId, String datatype, String name, String priceMin, String priceMax, HttpServletRequest request){
		if(StringUtils.isBlank(pageNumber)) {
			pageNumber = "0";
		}
		Page page = this.orderService.selectOrderInfoPage(Integer.parseInt(pageNumber), orderId, datatype, name, priceMin, priceMax);
		return page;
	}
	
	/**
	 * 后台查询订单详情
	 * @param orderId	订单Id
	 * @return
	 */
	@RequestMapping("/backDetail")
    public ModelAndView selectOrderDetailByBack(String orderId, HttpServletRequest request){
		ModelAndView mv = new ModelAndView("/background/order/orderdetail");
		BfOrder order = this.orderService.selectOrderDetail(orderId);
		mv.addObject("order", order);
		return mv;
	}
	
	/**
	 * 修改订单状态
	 * @param orderId	订单Id
	 * @param status	订单状态
	 * @return
	 */
	@RequestMapping("/updateStatus")
    public @ResponseBody boolean updateOrderStatus(String orderId, int status, HttpServletRequest request){
		BfOrder order = this.orderService.selectOrderDetail(orderId);
		order.setStatus(status);
		this.orderService.updateOrder(order);
		return true;
	}
}
